[PHP] Weird problem with dynamic method invocation

Posted by Rolf on Stack Overflow See other posts from Stack Overflow or by Rolf
Published on 2010-04-23T11:52:56Z Indexed on 2010/04/23 12:03 UTC
Read the original article Hit count: 272

Filed under:
|
|
|

Hi everyone,

this time, I'm facing a really weird problem. I've the following code:

$xml = simplexml_load_file($this->interception_file);
foreach($xml->children() as $class) {
    $path = str_replace('__CLASS_DIR__',CLASS_DIR,$class['path']);
    if(!is_file($path)) {
       throw new Exception('Bad configuration: file '.$path.' not found');
    }
    $className = pathinfo($path,PATHINFO_FILENAME);
    foreach($class as $method) {
       $method_name = $method['name'];
       $obj = new $className();
       var_dump(in_array($method_name,get_class_methods($className)));exit;
       echo $obj->$method_name();### not a method ???
    }
}

As you can see, I get the class name and method name from an XML file. I can create an instance of the class without any problem. The var_dump at the end returns true, that means $method_name (which has 2 optional parameters) is a method of $className.

BUT, and I am pretty sure the syntax is correct, when I try: $obj->$method_name() I get:

Fatal error: Method name must be a string

If you have any ideas, pleaaaaase tell me :) Thanks in advance, Rolf

© Stack Overflow or respective owner

Related posts about php

Related posts about dynamic